import 'package:flutter/material.dart';
import '../../helper/cacheHelper.dart';
import '../../pages/station/station_helper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

import '../../model/station.dart';
import '../../model/stationequip.dart';

class StationInfoEditPage extends StatefulWidget {
  @override
  StationInfoEditPage(this.station1, this.added);
  final Station station1;
  final VoidCallback added;

  @override
  StationInfoEditPageState createState() => new StationInfoEditPageState();
}

class StationInfoEditPageState extends State<StationInfoEditPage> {
  late Station s1;
  late List<String> TypeList;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    s1 = widget.station1;

    c1.text = s1.Code.toString();
    c2.text = s1.Location;
    c3.text = s1.STTP;
    c4.text = s1.DuanXin;
    c5.text = s1.WeiXing;
    c6.text = s1.ManaName;
    c7.text = s1.ManaPhone;
  }

  upload(BuildContext context) async {
    s1.Code = int.parse(c1.text);
    s1.Location = c2.text;
    s1.STTP = c3.text;
    s1.DuanXin = c4.text;
    s1.WeiXing = c5.text;
    s1.ManaName = c6.text;
    s1.ManaPhone = c7.text;

    await Station.Update(s1);

    if (widget.added != null) widget.added();

    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(
        // title: bottomTabs[currentIndex].title,
        title: Text("测站编辑"),
        // leading: Icon(Icons.menu),
      ),
      body: StationInfoEditBody(context),
    );
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];

    List<Widget> list2 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.RiverImage(),
        title: Text(
          widget.station1.STNM,
        ),
      ),
    ));

    list2.add(InputWidget.getInput("测站站号", c1));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("所在位置", c2));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("观测项目", c3));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("短信号码", c4));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("卫星号码", c5));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("看管人姓名", c6));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("看管人电话", c7));
    list2.add(Divider(
      height: 0.0,
    ));

    list1.add(InputWidget.inCard2(list2));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("保存", () => upload(context))));

    return list1;
  }

  StationInfoEditBody(BuildContext context) => SingleChildScrollView(
        child: StationInfoEditFields(context),
      );

  StationInfoEditFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
